package com.shuoyue.ycgk.ui.account;

import android.app.Activity;
import android.content.Intent;
import android.os.CountDownTimer;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.core.content.ContextCompat;

import com.shuoyue.ycgk.Constant;
import com.shuoyue.ycgk.R;
import com.shuoyue.ycgk.appcatch.MemeryCatch;
import com.shuoyue.ycgk.appcatch.SPUtils;
import com.shuoyue.ycgk.base.BaseMvpActivity;
import com.shuoyue.ycgk.entity.UserInfo;
import com.shuoyue.ycgk.ui.account.login.WeChatLogin;
import com.shuoyue.ycgk.ui.common.CstWebActivity;
import com.shuoyue.ycgk.ui.main.MainTabActivity;

import org.greenrobot.eventbus.EventBus;

import butterknife.BindView;
import butterknife.OnClick;

public class SMSLoginActivity extends BaseMvpActivity<AccountContract.Presenter>
        implements AccountContract.View, SmsContract.View {

    static int srequestCode = -1;
    public final static int REQUEST_CODE = 1543;
    @BindView(R.id.phone_num)
    EditText phoneNum;
    @BindView(R.id.edit_code)
    EditText editCode;
    @BindView(R.id.app_deal)
    TextView appDeal;
    @BindView(R.id.getCode)
    TextView getCode;
    private TimeCount time;
    SmsContract.Presenter smsPresenter;

    public static void start(Activity context) {
        context.startActivityForResult(new Intent(context, SMSLoginActivity.class).putExtra("srequestCode", REQUEST_CODE),
                REQUEST_CODE);

    }

    /**
     * 初始化layouy
     */
    @Override
    protected int getLayoutId() {
        return R.layout.activity_smslogin;
    }

    /**
     * 初始化布局
     */
    @Override
    protected void initView() {
        UserInfo userInfo = SPUtils.getUserInfo(mContext);
        if (userInfo != null) {
            MemeryCatch.token = userInfo.getToken();
            startActivity(new Intent(mContext, MainTabActivity.class));
            finish();
        }
        time = new TimeCount(60000, 1000);
    }

    @Override
    public void initData() {
        srequestCode = getIntent().getIntExtra("srequestCode", -1);
        super.initData();
        smsPresenter = new SmsContract.Presenter();
        smsPresenter.attachView(this);
        mPresenter = new AccountContract.Presenter();
        mPresenter.attachView(this);
    }

    @OnClick({R.id.back, R.id.app_deal, R.id.pwd, R.id.wechat, R.id.getCode, R.id.submit})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;
            case R.id.app_deal:
                CstWebActivity.start(mContext,
                        Constant.HOST_INTERFACE
                                + "/api/share/agreement", "用户协议");
                break;

            case R.id.pwd:
                startActivity(new Intent(mContext, AccountLoginActivity.class));
                finish();
                break;
            case R.id.wechat:
                WeChatLogin.login(this, (openId, accesstoken) -> mPresenter.wechatLogin(openId, accesstoken, null, null));
                break;
            case R.id.getCode:
                String phone = phoneNum.getText().toString();
                if (TextUtils.isEmpty(phone)) {
                    toast("请输入手机号码");
                    return;
                }
                smsPresenter.sendSms(phone);
                break;
            case R.id.submit:
                login();
                break;
        }
    }

    void login() {
        String phone = phoneNum.getText().toString();
        String sms = editCode.getText().toString();
        if (sms.length() == 0) {
            toast("请输入验证码");
            return;
        }
        mPresenter.smslogin(phone, sms, null, null);
    }

    /**
     * 发送验证码后
     */
    @Override
    public void sendSuc(String phone) {
        toast("验证码发送成功");
        time.start();
    }

    @Override
    protected void onDestroy() {
        EventBus.getDefault().unregister(this);
        super.onDestroy();
        if (time != null) {
            time.cancel();
        }
    }

    @Override
    public void loginSuc(UserInfo userInfo) {
        if (userInfo != null) {
            MemeryCatch.token = userInfo.getToken();
            SPUtils.updateUserInfo(mContext, userInfo);
            if (srequestCode == -1) {
                startActivity(new Intent(mContext, MainTabActivity.class));
                finish();
            } else {
                setResult(RESULT_OK);
                finish();
            }
        }
    }

    @Override
    public void registSuc() {

    }

    @Override
    public void registFail(String msg) {

    }

    @Override
    public void resetSuc() {

    }

    @Override
    public void bindPhone(String openId, String accessToken) {
        BindPhoneActivity.start(this, openId, accessToken);
    }

    class TimeCount extends CountDownTimer {

        public TimeCount(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);
        }

        @Override
        public void onTick(long millisUntilFinished) {
//            getCode.setBackgroundResource(R.drawable.bg_login_send_code_a);
            getCode.setTextColor(ContextCompat.getColor(mContext, R.color.themeColor));
            getCode.setEnabled(false);
            getCode.setText(millisUntilFinished / 1000 + "s");
        }

        @Override
        public void onFinish() {
//            getCode.setBackgroundResource(R.drawable.bg_login_send_code);
            getCode.setTextColor(ContextCompat.getColor(mContext, R.color.all_orange_color));
            getCode.setEnabled(true);
            getCode.setText("重新获取");
        }
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == BindPhoneActivity.REQUEST_CODE) {
            if (resultCode == RESULT_OK) {
                if (srequestCode == -1) {
                    startActivity(new Intent(mContext, MainTabActivity.class));
                    finish();
                } else {
                    setResult(RESULT_OK);
                    finish();
                }
            }
        }
    }
}
